-
-
Notifications
You must be signed in to change notification settings - Fork 131
Cache compiled static assets in service worker #2272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hkarani
wants to merge
1
commit into
stackernews:master
Choose a base branch
from
hkarani:cache-compiled-static-assets
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68b9158
to
1d081f5
Compare
1d081f5
to
886176f
Compare
886176f
to
d7bb007
Compare
d7bb007
to
fc85fa9
Compare
fc85fa9
to
c142b36
Compare
c142b36
to
1c450d0
Compare
1c450d0
to
b7f59e7
Compare
b7f59e7
to
c47243a
Compare
c47243a
to
a63f48f
Compare
a63f48f
to
1caa255
Compare
1caa255
to
2f9ccd5
Compare
@ekzyis I've implemented a new way to do this mainly because the service worker will only cache items in precache-manifest.json when the build starts, even updating precache-manifest.json mid-webpack build will still cache assets passed when webpack starts. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
A clear and concise description of what you changed and why.
This change patches the cached items array in the service worker at public/sw.js to include Next's compiled static assets emitted during build phase.
WHY?
The service worker ONLY includes the assets in precache-manifest.json injected by Workbox plugin when the Webpack build starts but our static assets only exist later in the build.
Static assets cannot be added in the server build , they are emitted in the client build phase(docs) and are not needed in dev.
However since the precache-manifest.json assets are hardcoded in an array in the generated service worker by Worbox in public/sw.js. We patch it to include our compiled assets after Next compiles and emits the files.
Screenshots
Where workbox stores the items in precache-manifest.json in Q=JSON.parse('the-precache-manifest.json') in the service worker.

Additional Context
Cached items are referenced as /_next/static/chunks/pages/offline-eb65c6c23790b89c.js" depending on the build version.
They static assets urls are formatted to be web urls for them to be accessible by the browser after caching.
Was anything unclear during your work on this PR? Anything we should definitely take a closer look at?
How to test a prod build?
Checklist
Are your changes backward compatible? Please answer below:
For example, a change is not backward compatible if you removed a GraphQL field or dropped a database column.
On a scale of 1-10 how well and how have you QA'd this change and any features it might affect? Please answer below:
7
This change adds the cached static assets in .next/static folder to the service worker in public/sw.js. The changes can be replicated by running:
then checking the changes of the public/sw.js in the editor just after Next completes the compilation phase.
To test dev build run:
For frontend changes: Tested on mobile, light and dark mode? Please answer below:
This change will affect the cached offline page.
Did you introduce any new environment variables? If so, call them out explicitly here:
No